<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Award2
{
    protected $order;
    protected $user;

    protected $amount;

    protected $set;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->amount = $this->order->total_pay - $this->order->total_freight;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        $is_repeat = ProductArea::checkRepeat($product_id);

        // 第二套
        $is_bao2 = ProductArea::checkNew1($product_id);
        $is_repeat2 = ProductArea::checkNew2($product_id);

        if ($is_bao) {
            $this->_bao();
        }

        if ($is_repeat) {
            $this->_repeat();
        }

        if ($is_bao2) {
            $this->_bao2();
        }

        if ($is_repeat2) {
            $this->_repeat2();
        }

        // 记录产品分区, 年分红使用
        if ($is_bao || $is_repeat) {
            $this->order->product_area = 1;
        }

        // 记录是否为第二套系统
        if ($is_bao2 || $is_repeat2) {
            $this->order->is_second = 1;

            $this->order->product_area = 2;
        }

        $this->order->save();

    }

    protected function _bao()
    {
        $type1 = 11;
        $name1 = '直推';
        $type2 = 12;
        $name2 = '间推';

        $r = Chain::getReferrer($this->user->id, true);
        if ($r) {
            if ($r->level >= UserLevel::$keys['vip1']) {
                // $count = (int) User::find()
                //     ->where(['referrer_id' => $r->id])
                //     ->andWhere(['level' => [
                //         UserLevel::$keys['vip1'],
                //         UserLevel::$keys['vip2'],
                //     ]])
                //     ->count();
                $ids = Chain::getChildren($r->id);
                $ids = ArrayHelper::getColumn($ids, 'id');
                $count = (int)Order::find()
                    ->where(['user_id' => $ids])
                    ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
                    ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
                    ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
                    ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
                    ->count();
                // 加上本人(用订单算, 就不用加了)
                // $count += 1;
                // var_dump($count);exit;

                if ($count >= 4) {
                    $commission = $this->set->recommend4;
                }
                else if ($count == 3) {
                    $commission = $this->set->recommend3;
                }
                else if ($count == 2) {
                    $commission = $this->set->recommend2;
                }
                else if ($count == 1) {
                    $commission = $this->set->recommend1;
                }
                $this->saveCommission($r->id, $commission, $type1, $name1);
            }
            $r2 = Chain::getReferrer($r->id, true);
            if ($r2 && $r2->level >= UserLevel::$keys['vip1']) {
                $commission = $this->amount * $this->set->second / 100;
                $this->saveCommission($r2->id, $commission, $type2, $name2);
            }
        }
    }

    protected function _repeat()
    {
        $type1 = 21;
        $name1 = '复购直推';
        $type2 = 22;
        $name2 = '复购间推';

        $r = Chain::getReferrer($this->user->id, true);
        if ($r) {
            if ($r->level >= UserLevel::$keys['vip1']) {
                $commission = $this->set->repeat1 * $this->amount / 100;
                $this->saveCommission($r->id, $commission, $type1, $name1);
            }
            $r2 = Chain::getReferrer($r->id, true);
            if ($r2 && $r2->level >= UserLevel::$keys['vip1']) {
                $commission = $this->amount * $this->set->repeat2 / 100;
                $this->saveCommission($r2->id, $commission, $type2, $name2);
            }
        }
    }

    protected function _bao2()
    {
        $type1 = 31;
        $name1 = '直推';
        $r = Chain::getReferrer($this->user->id, true);
        if ($r) {

            if ($r->new_level == UserLevel::$keys['vip22']) {
                $commission = $this->amount * $this->set->s2_recommend_2 / 100;
                $this->saveCommission($r->id, $commission, $type1, $name1);
            }


            if ($r->new_level == UserLevel::$keys['vip21']) {
                $commission = $this->amount * $this->set->s2_recommend / 100;
                $this->saveCommission($r->id, $commission, $type1, $name1);
            }


            // 平级奖
            // if ($r->new_level == UserLevel::$keys['vip24']) {
            //     $this->_equal($r);
            // }
        }
    }

    // protected function _repeat2()
    // {
    //     $type1 = 41;
    //     $name1 = '复购直推';
    //     $type2 = 42;
    //     $name2 = '复购间推';
    //
    //     $r = Chain::getReferrer($this->user->id, true);
    //     if ($r) {
    //
    //         // 平级只返1次
    //         $has_equal = false;
    //
    //         if ($r->new_level >= UserLevel::$keys['vip21']) {
    //             if ($r->new_level >= UserLevel::$keys['vip24']) {
    //                 $ratio = $this->set->s2_vip_repeat1;
    //             } else {
    //                 $ratio = $this->set->s2_repeat1;
    //             }
    //             $commission = $ratio * $this->amount / 100;
    //             $this->saveCommission($r->id, $commission, $type1, $name1);
    //
    //             // 平级奖
    //             if ($r->new_level == UserLevel::$keys['vip24']) {
    //                 $this->_equal($r);
    //                 $has_equal = true;
    //             }
    //         }
    //         $r2 = Chain::getReferrer($r->id, true);
    //         if ($r2 && $r2->new_level >= UserLevel::$keys['vip21']) {
    //             if ($r->new_level >= UserLevel::$keys['vip24']) {
    //                 $ratio = $this->set->s2_vip_repeat2;
    //             } else {
    //                 $ratio = $this->set->s2_repeat2;
    //             }
    //             $commission = $ratio * $this->amount / 100;
    //             $this->saveCommission($r2->id, $commission, $type2, $name2);
    //
    //             if (!$has_equal && $r2->new_level == UserLevel::$keys['vip24']) {
    //                 $this->_equal($r2);
    //             }
    //         }
    //     }
    // }
    protected function _repeat2()
    {
        $type1 = 41;
        $name1 = '复购直推';
        // $type2 = 42;
        // $name2 = '复购间推';

        $r = Chain::getReferrer($this->user->id, true);
        if ($r) {

            if ($r->new_level == UserLevel::$keys['vip22']) {
                $commission = $this->amount * $this->set->s2_repeat_2 / 100;
                $this->saveCommission($r->id, $commission, $type1, $name1);
            }

            else if ($r->new_level == UserLevel::$keys['vip21']) {
                $commission = $this->amount * $this->set->s2_repeat / 100;
                $this->saveCommission($r->id, $commission, $type1, $name1);
            }

        }
    }

    // 平级奖
    protected function _equal($user)
    {
        $type = 50;
        $name = '平级';
        $r = Chain::getReferrer($user->id, true);
        if ($r && $r->new_level == UserLevel::$keys['vip24']) {
            $commission = $this->amount * $this->set->s2_vip_equal / 100;
            $this->saveCommission($r->id, $commission, $type, $name);
        }
    }

    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->user->id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            // $model->status = $status;
            // $model->status = 1;
            $model->status = 0;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                // $account->balance += $balance;
                // 改成待返
                $account->await_balance += $balance;
                $account->save();
            }
        }
    }
}